package services

import (
	"context"
	"github.com/golang/protobuf/ptypes/empty"
	"github.com/golang/protobuf/ptypes/wrappers"
	context2 "kop/framework/context"
	"kop/framework/errors"
	"kop/modules/award/models"
	"kop/modules/mail/client"
	roleC "kop/modules/role/client"
	"kop/pb"
	"kop/pb/service"
)

type MailService struct {
	context2.Context
}

func (s *MailService) GetMails(ctx context.Context, req *wrappers.Int32Value) (*pb.Mails, error) {

	const PageNum = 1000

	var roleID = s.GetRoleID(ctx)
	return client.MailService.GetMails(context.TODO(), &service.PageRequest{RoleID: roleID, Page: req.Value, PageNum: PageNum})
}

func (s *MailService) Read(ctx context.Context, req *wrappers.Int32Value) (*empty.Empty, error) {

	var roleID = s.GetRoleID(ctx)
	return client.MailService.Read(context.TODO(), &service.DefaultRequest{RoleID: roleID, Value: req.Value})
}

func (s *MailService) Attachment(ctx context.Context, req *wrappers.Int32Value) (*pb.Award, error) {

	var roleID = s.GetRoleID(ctx)
	var role = roleC.GetRole(roleID)

	var award, err = client.MailService.Attachment(context.TODO(), &service.DefaultRequest{RoleID: roleID, Value: req.Value})
	if err != nil {
		return award, err
	}

	return award, models.SendAward(role, award)
}

func (s *MailService) Delete(ctx context.Context, req *pb.Int32Slice) (*empty.Empty, error) {

	if len(req.Slices) == 0 {
		return &empty.Empty{}, errors.ParamError
	}

	var roleID = s.GetRoleID(ctx)
	return client.MailService.Delete(context.TODO(), &service.DeleteMailRequest{RoleID: roleID, IDs: req.Slices})
}
